package com.sgcc.lms.openstack.controller;

import com.sgcc.lms.openstack.exception.NullTokenException;
import com.sgcc.lms.openstack.service.NeutronService;
import com.sgcc.lms.openstack.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.network.*;
import org.openstack4j.openstack.networking.domain.NeutronPort;
import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup;
import org.openstack4j.openstack.networking.domain.NeutronSecurityGroupRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-07-22 10:18
 * @Description: 网络服务控制层
 */
@RestController
@Slf4j
@Api(tags = "NeutronController：网络服务")
public class NeutronController {
    @Autowired
    private NeutronService neutronService;

    private static final String LINE = "==========================================》{}";

    /**
     * @Author 李孟帅
     * @Description 列出网络
     * @Date 2020/7/22
     **/
    @GetMapping("/networks")
    @ApiOperation("列出网络")
    public ResultUtil<?> networks(HttpServletRequest request) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        List<? extends Network> networks = neutronService.networks(tokenId);
        log.info(uri + ",列出网络" + LINE, "成功");
        return ResultUtil.success(networks);
    }

    /**
     * @Author 李孟帅
     * @Description 获取网络
     * @Date 2020/7/22
     **/
    @GetMapping("/networks/get")
    @ApiOperation("获取网络")
    public ResultUtil<?> getNetwork(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Network network = neutronService.getNetwork(tokenId, id);
        log.info(uri + ",获取网络" + LINE, "成功");
        return ResultUtil.success(network);
    }

    /**
     * @Author 李孟帅
     * @Description 列出子网
     * @Date 2020/7/22
     **/
    @GetMapping("/subnets")
    @ApiOperation("列出子网")
    public ResultUtil<?> subnets(HttpServletRequest request) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        List<? extends Subnet> subnets = neutronService.subnets(tokenId);
        log.info(uri + ",列出子网" + LINE, "成功");
        return ResultUtil.success(subnets);
    }

    /**
     * @Author 李孟帅
     * @Description 获取子网
     * @Date 2020/7/22
     **/
    @GetMapping("/subnets/get")
    @ApiOperation("获取子网")
    public ResultUtil<?> getSubnet(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Subnet subnet = neutronService.getSubnet(tokenId, id);
        log.info(uri + ",获取子网" + LINE, "成功");
        return ResultUtil.success(subnet);
    }

    /**
     * @Author 李孟帅
     * @Description 列出端口
     * @Date 2020/7/22
     **/
    @GetMapping("/ports")
    @ApiOperation("列出端口")
    public ResultUtil<?> ports(HttpServletRequest request) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        List<? extends Port> ports = neutronService.ports(tokenId);
        log.info(uri + ",列出端口" + LINE, "成功");
        return ResultUtil.success(ports);
    }

    /**
     * @Author 李孟帅
     * @Description 获取端口
     * @Date 2020/7/22
     **/
    @GetMapping("/ports/get")
    @ApiOperation("获取端口")
    public ResultUtil<?> getPort(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Port port = neutronService.getPort(tokenId, id);
        log.info(uri + ",获取端口" + LINE, "成功");
        return ResultUtil.success(port);
    }

    /**
    * @Author 李孟帅
    * @Description 创建端口
    * @Date 2020/7/23
    **/
    @PostMapping("/ports/create")
    @ApiOperation("创建端口")
    public ResultUtil<?> createPort(HttpServletRequest request, @RequestBody NeutronPort port) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Port port1 = neutronService.createPort(tokenId, port);
        log.info(uri + ",创建端口" + LINE, "成功");
        return ResultUtil.success(port1);
    }

    /**
     * @Author 李孟帅
     * @Description 删除端口
     * @Date 2020/7/22
     **/
    @GetMapping("/ports/delete")
    @ApiOperation("删除端口")
    public ResultUtil<?> deletePort(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        ActionResponse actionResponse = neutronService.deletePort(tokenId, id);
        log.info(uri + ",删除端口" + LINE, "成功");
        return ResultUtil.success(actionResponse);
    }

    /**
     * @Author 李孟帅
     * @Description 列出路由器
     * @Date 2020/7/22
     **/
    @GetMapping("/routers")
    @ApiOperation("列出路由器")
    public ResultUtil<?> routers(HttpServletRequest request) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        List<? extends Router> routers = neutronService.routers(tokenId);
        log.info(uri + ",列出路由器" + LINE, "成功");
        return ResultUtil.success(routers);
    }

    /**
     * @Author 李孟帅
     * @Description 获取路由器
     * @Date 2020/7/22
     **/
    @GetMapping("/routers/get")
    @ApiOperation("获取路由器")
    public ResultUtil<?> getRouter(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Router router = neutronService.getRouter(tokenId, id);
        log.info(uri + ",获取路由器" + LINE, "成功");
        return ResultUtil.success(router);
    }

    /**
     * @Author 李孟帅
     * @Description 列出安全组
     * @Date 2020/7/23
     **/
    @GetMapping("/securityGroups")
    @ApiOperation("列出安全组")
    public ResultUtil<?> securityGroups(HttpServletRequest request) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        List<? extends SecurityGroup> securityGroups = neutronService.securityGroups(tokenId);
        log.info(uri + ",列出安全组" + LINE, "成功");
        return ResultUtil.success(securityGroups);
    }

    /**
     * @Author 李孟帅
     * @Description 获取安全组
     * @Date 2020/7/23
     **/
    @GetMapping("/securityGroups/get")
    @ApiOperation("获取安全组")
    public ResultUtil<?> getSecurityGroup(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        SecurityGroup securityGroup = neutronService.getSecurityGroup(tokenId, id);
        log.info(uri + ",获取安全组" + LINE, "成功");
        return ResultUtil.success(securityGroup);
    }

    /**
     * @Author 李孟帅
     * @Description 创建安全组
     * @Date 2020/7/23
     **/
    @PostMapping("/securityGroups/create")
    @ApiOperation("创建安全组")
    public ResultUtil<?> createSecurityGroups(HttpServletRequest request, @RequestBody NeutronSecurityGroup securityGroup) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        SecurityGroup securityGroup1 = neutronService.createSecurityGroup(tokenId, securityGroup);
        log.info(uri + ",创建安全组" + LINE, "成功");
        return ResultUtil.success(securityGroup1);
    }

    /**
     * @Author 李孟帅
     * @Description 删除安全组
     * @Date 2020/7/23
     **/
    @GetMapping("/securityGroups/delete")
    @ApiOperation("删除安全组")
    public ResultUtil<?> deleteSecurityGroups(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        ActionResponse actionResponse = neutronService.deleteSecurityGroup(tokenId, id);
        log.info(uri + ",删除安全组" + LINE, "成功");
        return ResultUtil.success(actionResponse);
    }

    /**
     * @Author 李孟帅
     * @Description 更新安全组
     * @Date 2020/7/23
     **/
    @PostMapping("/securityGroups/update")
    @ApiOperation("更新安全组")
    public ResultUtil<?> updateSecurityGroups(HttpServletRequest request, @RequestBody NeutronSecurityGroup securityGroup) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        SecurityGroup securityGroup1 = neutronService.updateSecurityGroup(tokenId, securityGroup);
        log.info(uri + ",更新安全组" + LINE, "成功");
        return ResultUtil.success(securityGroup1);
    }

    /**
     * @Author 李孟帅
     * @Description 列出安全组规则
     * @Date 2020/7/23
     **/
    @GetMapping("/securityGroupRules")
    @ApiOperation("列出安全组规则")
    public ResultUtil<?> securityGroupRules(HttpServletRequest request) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        List<? extends SecurityGroupRule> securityGroupRules = neutronService.securityGroupRules(tokenId);
        log.info(uri + ",列出安全组规则" + LINE, "成功");
        return ResultUtil.success(securityGroupRules);
    }

    /**
     * @Author 李孟帅
     * @Description 获取安全组规则
     * @Date 2020/7/23
     **/
    @GetMapping("/securityGroupRules/get")
    @ApiOperation("获取安全组规则")
    public ResultUtil<?> getSecurityGroupRules(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        SecurityGroupRule securityGroupRule = neutronService.getSecurityGroupRule(tokenId, id);
        log.info(uri + ",获取安全组规则" + LINE, "成功");
        return ResultUtil.success(securityGroupRule);
    }

    /**
     * @Author 李孟帅
     * @Description 删除安全组规则
     * @Date 2020/7/23
     **/
    @GetMapping("/securityGroupRules/delete")
    @ApiOperation("删除安全组规则")
    public ResultUtil<?> deleteSecurityGroupRules(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        neutronService.deleteSecurityGroupRule(tokenId, id);
        log.info(uri + ",删除安全组规则" + LINE, "成功");
        return ResultUtil.success();
    }

    /**
     * @Author 李孟帅
     * @Description 创建安全组规则
     * @Date 2020/7/23
     **/
    @PostMapping("/securityGroupRules/create")
    @ApiOperation("创建安全组规则")
    public ResultUtil<?> createSecurityGroupRules(HttpServletRequest request, @RequestBody NeutronSecurityGroupRule securityGroupRule) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        SecurityGroupRule securityGroupRule1 = neutronService.createSecurityGroupRule(tokenId, securityGroupRule);
        log.info(uri + ",创建安全组规则" + LINE, "成功");
        return ResultUtil.success(securityGroupRule1);
    }
}






















